/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author RAI+ruben.arenas
 */
import java.net.*;
import java.io.*;



public class MiCliente extends Thread{
	       MulticastSocket s;
	       String address;
               InetAddress group;
	       int socketNumber;
               String mensaje;
               boolean escribir;
               boolean terminar;

//*************************************************
public MiCliente(String str){
          super(str);
}
//*************************************************
public void setMensaje(String msg){
this.mensaje=msg;
}
//*************************************************
public String getMensaje(){
return(this.mensaje);
}
//*************************************************
public void run(){
 try {

	       String address = "224.0.0.100";
               InetAddress group = InetAddress.getByName(address);
	       this.socketNumber = 2345;
               s = new MulticastSocket(socketNumber);
               s.joinGroup(group);
               
               this.escribir=false;
               this.terminar=false;
               
	       String CurLine = "";
  
            
                
   while(!this.terminar){
        
       while(!this.escribir){}   
            this.escribir=false;
            CurLine = this.getMensaje();
            
            //dejamos un espacio en blanco que nos servirá para codificar
            //el tamaño del mensaje, y así implementar el protocolo
            String mensajeProtocolo = " " + CurLine;
            byte [] m = mensajeProtocolo.getBytes();
            //m[0]=(byte)CurLine.length();

            DatagramPacket messageOut = new DatagramPacket(m, m.length, group, socketNumber);
            s.send(messageOut);
            
            System.out.println("\n\n<-------------------------------------------");
            System.out.println("Sent:" + new String(messageOut.getData()));
            System.out.println("------------------------------------------->");        
   }             

	    s.leaveGroup(group);
            System.out.println("SALIENDO DEL CLIENTE.");

     }
     catch (SocketException e)	{ System.out.println("Socket: " + e.getMessage());  }
     catch (IOException e)	{ System.out.println("IO: " + e.getMessage());      }
     finally 		 	{ if(s != null) s.close();	}

}
//*************************************************

}